//
//  SCWorkoutDataBlock.m
//  FirstSCAnalyzer
//
//  Created by Roelant Ossewaarde on 8/28/09.
//  Copyright 2009 SUNY Buffalo. All rights reserved.
//

#import "SCWorkoutDataBlock.h"


@implementation SCWorkoutDataBlock

@synthesize time;
@synthesize distance;
@synthesize cumulativeDistance;
@synthesize averageStrokeRate;
@synthesize numberOfStrokes;
@synthesize heartRate;
@synthesize intervalType;
@synthesize unitType;
@synthesize date;
@synthesize label;
@synthesize	sequenceno;
@synthesize	relativeSequenceno;
@synthesize memType;

-(id) init {
	self = [super init];
	self.distance = 0;
	self.time = 0; // = 4 minutes
	self.averageStrokeRate = 0;
	self.numberOfStrokes = 0;
	self.intervalType = DistanceIntervalType;
	self.label = nil;
	self.memType = UnknownmemType;
	self.date = nil;
	[self setSequenceno:-1];
	return self;
}

-(id) initWithLabel:(NSString *)myLabel {
	[self init];
	self.label = myLabel;
	return self;
}

-(void) setWorkoutData:(Chunk *)chunk {
	if ([chunk typeOfChunk] == AutomemWorkoutChunkType) {
		[self setDistance:[chunk getDistance]];
		[self setTime:[chunk getTime]];
		[self setAverageStrokeRate:[chunk getAverageStrokeRate]];
		[self setNumberOfStrokes:[chunk getNumberOfStrokes]];
		//[self setIntervalType:[chunk getIntervalType]];
		
	}
}
	

-(void) logDump {
	NSLog(@"Logdump of workout [%@]: ", [self label]);
	NSLog(@"sequence no: %i", [self sequenceno]);
	NSLog(@"distance: %@", [self distance]);
	NSLog(@"time: %i", [self time]);
	NSLog(@"averageStrokeRate: %i", [self averageStrokeRate]);
	NSLog(@"numberOfStrokes: %i", [self numberOfStrokes]);
	NSLog(@"intervalType: %i", [self intervalType]);
}

-(NSString *) intToCsv:(int)num paren:(NSString *)parenString{
	return [NSString stringWithFormat:@"%@%i%@", parenString, num, parenString];
}

-(NSString *) floatToCsv:(float)num paren:(NSString *)parenString{
	return [NSString stringWithFormat:@"%@%.2f%@", parenString, num, parenString];
}

-(NSString *) dateToCsv:(NSDate *)thedate paren:(NSString *)parenString {
	return [NSString stringWithFormat:@"%@%@%@", parenString, [date description], parenString];
}

-(NSString *) nsstringToCsv:(NSString *)str paren:(NSString *)parenString {
	return [NSString stringWithFormat:@"%@%@%@", parenString, str, parenString];
}

-(NSString *) csvHeaderWithSeparator:(NSString *)sepString paren:(NSString *)parenString headerColumns:(CSVColumnType)hc {
	NSString *headerString = [[NSString alloc] initWithFormat:@"%@", [self nsstringToCsv:@"Sequence no" paren:parenString]];
	if (hc & CSVColumnMemtype)				headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Memory type" paren:parenString]];
	if (hc & CSVColumnRelativeSequenceNo)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Relative Sequence No" paren:parenString]];
	if (hc & CSVColumnDate)					headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Date" paren:parenString]];
	if (hc & CSVColumnDistance)				headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Distance (m)" paren:parenString]];
	if (hc & CSVColumnCumulativeDistance)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Cumulative Distance (m)" paren:parenString]];;
	if (hc & CSVColumnDuration)				headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Duration (s)" paren:parenString]];
	if (hc & CSVColumnSplittimeInSeconds)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"500m split (s)" paren:parenString]];
	if (hc & CSVColumnSplittimeInString)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"500m split (min)" paren:parenString]];
	if (hc & CSVColumnNumberOfStrokes)		headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Number of Strokes" paren:parenString]];
	if (hc & CSVColumnDistancePerStroke)		headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Distance Per Stroke" paren:parenString]];
	if (hc & CSVColumnAverageStrokeRate)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Strokerate" paren:parenString]];;
	if (hc & CSVColumnHeartRate)			headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:@"Heartrate" paren:parenString]];
	headerString = [headerString stringByAppendingFormat:@"\n"];
	return headerString;
	
}



-(NSString *) toCSVWithSeparator:(NSString *)sepString paren:(NSString *)parenString headerColumns:(CSVColumnType) hc {
	NSString *memtypeString;
	if ([self memType] == AutomemType) memtypeString = @"just row";
	else if ([self memType] == WorkoutmemType) memtypeString = @"workout";
	else memtypeString = @"unknown";
	
	float splittime = (((float)[self time]/64) / ((float)[self distance] / 16)) * 500;
	NSString *splittimeString = [NSString stringWithFormat:@"%02i:%02.1f", (int)splittime / 60, fmod(splittime, 60)];
	float dps = 0;
	if  ([self numberOfStrokes] > 0) dps = ((float)[self distance]/16) / [self numberOfStrokes];

	NSString *headerString = [[NSString alloc] initWithFormat:@"%@", [self intToCsv:[self sequenceno] paren:parenString]];
	if (hc & CSVColumnMemtype)				headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:memtypeString paren:parenString]];
	if (hc & CSVColumnRelativeSequenceNo)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self intToCsv:[self relativeSequenceno] paren:parenString]];
	if (hc & CSVColumnDate)					headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self dateToCsv:[self date] paren:parenString]];
	if (hc & CSVColumnDistance)				headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self floatToCsv:(float)[self distance]/16 paren:parenString]];
	if (hc & CSVColumnCumulativeDistance)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self floatToCsv:(float)[self cumulativeDistance]/16 paren:parenString]];
	if (hc & CSVColumnDuration)				headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self floatToCsv:(float)[self time]/64 paren:parenString]];
	if (hc & CSVColumnSplittimeInSeconds)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self floatToCsv:splittime paren:parenString]];
	if (hc & CSVColumnSplittimeInString)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self nsstringToCsv:splittimeString paren:parenString]];
	if (hc & CSVColumnNumberOfStrokes)		headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self intToCsv:[self numberOfStrokes] paren:parenString]];
	if (hc & CSVColumnDistancePerStroke)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self floatToCsv:dps paren:parenString]];
	if (hc & CSVColumnAverageStrokeRate)	headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self floatToCsv:(float)[self averageStrokeRate]/256 paren:parenString]];
	if (hc & CSVColumnHeartRate)			headerString = [headerString stringByAppendingFormat:@"%@%@", sepString, [self intToCsv:[self heartRate] paren:parenString]];
	headerString = [headerString stringByAppendingFormat:@"\n"];
	return headerString;
		


	/*NSString *headerString = [NSString stringWithFormat:@"%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@\n", 
							  [self nsstringToCsv:memtypeString paren:parenString], 
							  sepString,  
							  [self intToCsv:[self relativeSequenceno] paren:parenString], 
							  sepString,  
							  [self intToCsv:[self sequenceno] paren:parenString], 
							  sepString,  
							  [self dateToCsv:[self date] paren:parenString], 
							  sepString,  
							  [self floatToCsv:(float)[self distance]/16 paren:parenString], 
							  sepString, 
							  [self floatToCsv:(float)[self cumulativeDistance]/16 paren:parenString], 
							  sepString,
							  [self floatToCsv:(float)[self time]/64 paren:parenString], 
							  sepString,  
							  [self intToCsv:[self numberOfStrokes] paren:parenString], 
							  sepString,  
							  [self floatToCsv:(float)[self averageStrokeRate]/256 paren:parenString],
							  sepString,  
							  [self intToCsv:[self heartRate] paren:parenString]
							  ];
	return headerString;*/
}

-(NSString *) csvHeader {
	return [self csvHeaderWithSeparator:@"," paren:@"\"" headerColumns:CSVColumnAll];
}

-(NSString *) toCSV {
	return [self toCSVWithSeparator:@"," paren:@"\"" headerColumns:CSVColumnAll];
}
			
@end
